package com.tnl.app.dao;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.EmployeePerson;
import com.tnl.app.entity.Person;
import com.tnl.app.entity.PersonRecom;
import com.tnl.base.BaseDAO;

@Repository
public class PersonDAO {
	protected static final String SQL_SAVE = "insert into tb_person(openid,name,age,workage,"
			+ "phone,address,email,icon,"
			+ "image,bestdomain,active,createDate,"
			+ "inservice,companyID,  cityId, roleId,"
			+ " companyname, mobile, platformid,domainId,comment,intentTime) "
			+ "values(?,?,?,?"
			+ ",?,?,?,?" + ",?,?,?,?" + ",?,?,?,?" + ",?,?,?,?,?,?)";

	protected static final String SQL_UPDATE = "update tb_person set openid=?, name=?"
			+ ",age=?, workage=?,phone=?,address =?,email=?,icon=?,image=?,bestdomain=? "
			+ ", active=?, createDate=? ,inservice=?,companyID=?, cityid=?, roleid=?, companyname=?, mobile=?, "
			+ "roleName = ?, platformid=? where id =?";

	protected static final String SQL_GET_ALL = "select * from tb_person ";

	protected static final String SQL_GET_BY_PHONE = "select * from tb_person where phone = ?";

	protected static final String SQL_GET_LIST = "select * from tb_person where id in (";

	protected static final String SQL_GET = "select * from tb_person  where id = ?";
	
	protected static final String SQL_GET_PERSONRECOM_ID = "select * from tb_person_recom  where id = ?";
	
	protected static final String SQL_GET_PERSONRECOM="select * from tb_person_recom where phone=?";

	protected static final String SQL_DELETE = "delete from tb_person where id=?";

	protected static final String SQL_SAVE_PERSON_RECOM = "insert into tb_person_recom(openid,name,age,workage,"
			+ "phone,address,email,icon,"
			+ "image,bestdomain,active,createDate,"
			+ "inservice,companyID,  cityId, roleId,roleName,"
			+ " companyname, mobile, platformid,domainId,comment,intentTime) "
			+ "values(?,?,?,?"
			+ ",?,?,?,?" + ",?,?,?,?" + ",?,?,?,?,?" + ",?,?,?,?,?,?)";
	
	public int save(Person person) {
		if (person == null) {
			return -1;
		}
		Long id = BaseDAO.save(SQL_SAVE, person.getOpenid(), person.getName(),
				person.getAge(), person.getWorkage(), person.getPhone(),
				person.getAddress(), person.getEmail(), person.getIcon(),
				person.getImage(), person.getBestdomain(), person.getActive(),
				person.getCreateDate(), person.getInservice(),
				person.getCompanyId(), person.getCityId(), person.getRoleName(),
				person.getCompanyName(), person.getMobile(),
				person.getPlatformId(), person.getDomainId(),person.getComment(),person.getIntentTime());

		return id.intValue();
	}

	public int savePersonRecom(PersonRecom person) {
		if (person == null) {
			return -1;
		}
		Long id = BaseDAO.save(SQL_SAVE_PERSON_RECOM, person.getOpenid(), person.getName(),
				person.getAge(), person.getWorkage(), person.getPhone(),
				person.getAddress(), person.getEmail(), person.getIcon(),
				person.getImage(), person.getBestdomain(), person.getActive(),
				person.getCreateDate(), person.getInservice(),
				person.getCompanyId(), person.getCityId(), person.getRoleId(),person.getRoleName(),
				person.getCompanyName(), person.getMobile(),
				person.getPlatformId(), person.getDomainId(),person.getComment(),person.getIntentTime());

		return id.intValue();
	}


	
	public int delete(String... personIds) {
		if (personIds != null) {
			if (personIds.length == 1) {
				return BaseDAO.execute(SQL_DELETE, personIds[0]);
			} else {
				Object[][] params = new Object[personIds.length][1];
				int i = 0;
				for (String id : personIds) {
					params[i][0] = id;
				}

				int[] rows = BaseDAO.batch(SQL_DELETE, params);
				if (rows != null && rows.length > 0) {
					int total = 0;
					for (int r : rows) {
						total += r;
					}
					return total;
				} else {
					return -1;
				}
			}
		} else {
			return 0;
		}
	}

	public List<Person> get(String... Ids) {
		if (Ids != null) {
			StringBuilder sql = new StringBuilder(SQL_GET_LIST);
			boolean start = false;
			for (String id : Ids) {
				if (start) {
					sql.append(",").append(id);
				} else {
					start = true;
					sql.append(id);
				}
			}
			sql.append(")");
			return BaseDAO.queryForList(Person.class, sql.toString());

		} else {
			return Collections.emptyList();
		}
	}

	public List<Person> getALL() {
		return BaseDAO.queryForList(Person.class, SQL_GET_ALL);
	}

	protected static final String SQL_UPDATE1 = "update tb_person set openid=?, name=?"
			+ ",age=?, workage=?,phone=?,address =?,email=?,icon=?,image=?,bestdomain=? ,"
			+ " active=?, createDate=? ,inservice=?, companyID=?, companyname=?, cityid=?,cityname= ?,"
			+ " roleid=?, roleName=? , mobile=?, platformid=? where id =?";

	public int update(Person person) {
		return BaseDAO.execute(SQL_UPDATE1, person.getOpenid(),
				person.getName(), person.getAge(), person.getWorkage(),
				person.getPhone(), person.getAddress(), person.getEmail(),
				person.getIcon(), person.getImage(), person.getBestdomain(),
				person.getActive(), person.getCreateDate(),
				person.getInservice(), person.getCompanyId(),
				person.getCompanyName(), person.getCityId(),
				person.getCityName(), person.getRoleId(), person.getRoleName(),
				person.getMobile(), person.getPlatformId(), person.getId());
	}

	public Person getById(int personId) {
		return BaseDAO.queryForObject(Person.class, SQL_GET, personId);
	}
	
	public PersonRecom getByPersonRecomId(int personId) {
		return BaseDAO.queryForObject(PersonRecom.class, SQL_GET_PERSONRECOM_ID, personId);
	}
	
	
	public PersonRecom getByPersonRecomPhone(String phone){
		return BaseDAO.queryForObject(PersonRecom.class,SQL_GET_PERSONRECOM, phone);
				
	}

	public Person getByPhone(String phone) {
		return BaseDAO.queryForObject(Person.class, SQL_GET_BY_PHONE, phone);
	}

	public int delete(int personId) {
		return BaseDAO.execute(SQL_DELETE, personId);
	}

	protected static final String SQL_UPDATE_PROFILE = "update tb_person "
			+ " set  name=? ,  workage=?, bestdomain=? ,   "
			+ "companyID=?, companyname=?, cityid=?,cityname= ?,"
			+ " roleid=?, roleName=? , platformid=?,  domainId= ? , domainName=? ,image=? where id =?";

	protected static final String SQL_UPDATE_BASE_PROFILE= "update tb_person "
			+ " set name=?, workage=?,phone=?,email=?,cityid=?,cityname=?,comment=?,intentTime=? where id =?";
	public int updateProfile(Person person) {
		return BaseDAO.execute(SQL_UPDATE_PROFILE, person.getName(),
				person.getWorkage(), person.getBestdomain(),
				person.getCompanyId(), person.getCompanyName(),
				person.getCityId(), person.getCityName(), person.getRoleId(),
				person.getRoleName(), person.getPlatformId(),
				person.getDomainId(), person.getDomainName(),
				person.getImage(), person.getId());
	}
	
	public int updateBaseProfile(Person person) {
		return BaseDAO.execute(SQL_UPDATE_BASE_PROFILE, person.getName(),
				person.getWorkage(),person.getPhone(),person.getEmail(),person.getCityId(), person.getCityName(),person.getComment(),person.getIntentTime(),person.getId());
	}
	
	protected static final String UPDATE_PERSON_DOMAIN= "update tb_person "
			+ " set domainId=?, domainName=? where id =?";
	public int updateDomain(Person perosn) {
		return BaseDAO.execute(UPDATE_PERSON_DOMAIN, perosn.getDomainId(),
				perosn.getDomainName(), perosn.getId());
	}
	
	protected static final String UPDATE_PERSON_NOWPOSI="update tb_person "
	+ " set companyId=?, companyname=?,roleId=?,roleName=?,bestdomain=? where id =?";
	public int saveResumeNowPosi(Person person){
		return BaseDAO.execute(UPDATE_PERSON_NOWPOSI,person.getCompanyId(),person.getCompanyName(),person.getRoleId(),person.getRoleName(),person.getBestdomain(),person.getId());
	}

	public static final String SQL_SAVEPERSON="INSERT INTO `tb_person` "
			+ "(`openid`, `name`, `workage`, `phone`,  `createdate`, `companyId`, `companyname`, "
			+ "`domainId`, `domainName`, `roleId`, `roleName`, `active`, `inservice`, "
			+ "`cityId`, `cityName`, `mobile`, `platformid`, `bestdomain`, `updatedate`,  `roleFullName`) "
			+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	public int savePerson(EmployeePerson emp) {
		// TODO Auto-generated method stub
		return (int)BaseDAO.executeInsertGenKey(SQL_SAVEPERSON, emp.getOpenid(),emp.getName(),
				emp.getWorkage(),emp.getPhone(),new Date(),emp.getCompanyId(),emp.getCompanyName(),
				emp.getDomainId(),emp.getDomainName(),emp.getRoleId(),emp.getRoleName(),1,0,
				emp.getCityId(),emp.getCityName(),emp.getMobile(),1,emp.getBestdomain(),
				new Date(),emp.getRoleFullName());
	}

	public static final String SQL_UPDATEPERSON="update tb_person set name=?,workage=?,phone=?,"
			+ "companyId=?,companyName=?,domainId=?,domainName=?,roleId=?,roleName=?,"
			+ "cityId=?,cityName=?,mobile=?,bestdomain=?,updatedate=?,roleFullName=? where id =?";
	public int updatePerson(EmployeePerson emp) {
		// TODO Auto-generated method stub
		return BaseDAO.execute(SQL_UPDATEPERSON, emp.getName(),emp.getWorkage(),emp.getPhone(),emp.getCompanyId(),
				emp.getCompanyName(),emp.getDomainId(),emp.getDomainName(),emp.getRoleId(),emp.getRoleName(),
				emp.getCityId(),emp.getCityName(),emp.getMobile(),emp.getBestdomain(),new Date(),
				emp.getRoleFullName(),emp.getPersonId());
	}

}
